import { IHttpResponse, IObject } from "../types/interface";
import { Method } from "axios";
import http from "../utils/http";
import {getToken} from "@/utils/cache";

/**
 * 常用CRUD
 */
export default {
  /**
   * 删除
   * @param path
   * @param params
   * @returns
   */
  delete(path: string, params: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      data: params,
      headers: {"tokenId": getToken()},
      method: "DELETE"
    });
  },
  get(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return new Promise((resolve, reject) => {
      http({
        url: path,
        params,
        headers: {"tokenId": getToken()},
        method: "GET"
      })
          .then(resolve)
          .catch((error) => {
            if (error !== "-999") {
              reject(error);
            }
          });
    });
  },
  put(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      data: params,
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        "tokenId": getToken(),
        ...headers
      },
      method: "PUT"
    });
  },
  /**
   * 通用post方法
   * @param path
   * @param body
   * @returns
   */
  post(path: string, body?: IObject, headers?: IObject): Promise<IHttpResponse> {
    return http({
      url: path,
      method: "post",
      headers: {
        "Content-Type": "application/json;charset=UTF-8",
        "tokenId": getToken(),
        ...headers
      },
      data: body
    });
  },

  /**
   * 下载文件
   * @param {*} url
   * @param {*} params
   * @param {*} filename
   * @param {*} method
   */
  download(
      path: string,
      params?: IObject,
      filename?: string,
      method: Method = "get"
  ): Promise<IHttpResponse> {
    const opt: any = {url: path, params, method, responseType: "blob"};
    if (method.toLowerCase() === "post") {
      opt.data = params;
      delete opt.params;
    }

    return http(opt).then((res): any => {
      const a = document.createElement("a");
      const evt = document.createEvent("MouseEvents");
      evt.initEvent("click", false, false);
      a.download = filename || new Date().getTime().toString();
      a.href = URL.createObjectURL(res);
      a.dispatchEvent(evt);
    });
  },
  /**
   * 下载文件
   * @param {*} url
   * @param {*} params
   * @param {*} filename
   * @param {*} method
   */
  downloadOss(
      path: string,
      params?: IObject,
      filename?: string,
      method: Method = "get"
  ): Promise<IHttpResponse> {


    if (filename == null) {
      const lastSlashIndex = path.lastIndexOf('/');
      filename == path.substring(lastSlashIndex + 1);
    }
    path = "fii/front/user/download?url=" + path;
    return this.download(path, {}, filename);
  },

  /**
   * 身份证隐藏中间十位
   */
  hideIdCard(idCard: string): string {
    //验证身份证号必须满足18位，最后一位可以是字母
    if (!/^\d{17}(\d|x)$/i.test(idCard)) {
      return idCard;
    } else {
      return idCard.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
    }
  },

  /**
   * 隐藏手机号中间4位
   */
  hidePhone(phone: string): string {
    //验证手机号必须满足11位，且只能是数字
    if (!/^\d{11}$/.test(phone)) {
      return phone;
    } else {
      return phone.replace(/^(.{3})(?:\d+)(.{4})$/, "$1****$2");
    }
  },


  /**
   * 用户类型
   * @param value
   */
  userType(value?: string) {
    let result = '';
    if(value && value.includes('0')){
      result += '代表队负责人、';
    }
    if(value && value.includes('1')){
      result += '领队、';
    }
    if(value && value.includes('2')){
      result += '指导老师、';
    }
    if(value && value.includes('3')){
      result += '选手、';
    }
    //去掉最后一个逗号(如果不需要去掉，就不用写)
    if (result.length > 0) {
        result = result.substr(0, result.length - 1);
    }
    return result;
  },

  /**
   * 证件类型
   */
  certificateType(value?: string) {
    let result = '';
    if(value && value.includes('1')){
        result = '居民身份证';
    }
    if(value && value.includes('2')){
      result = '临时身份证';
    }
    if(value && value.includes('3')){
      result = '港澳居民来往内地通行证';
    }
    if(value && value.includes('4')){
      result = '台湾居民来往大陆通行证';
    }
    if(value && value.includes('5')){
      result = '护照/其他';
    }
    return result;
  },

  /**
   * 成绩转义显示
   *     得分    score
   *     用时    score1
   *     最终轮次    score2
   *     轮次净胜分    score3
   *     轮次(总)得分    score4
   *     创意分    score5
   *     音乐匹配分    score6
   *     编程分    score7
   *     侦查用时    score8
   */
  deCodeScore(code?: string) {
    let result = '';
    if(code && code.includes('score')){
        result = '得分';
    }
    if(code && code.includes('score1')){
        result = '用时';
    }
    return result;
  }


};
